// pages/buy/buy.js
var tools = require('../../utils/util.js');
const app = getApp();
var router = require("../../router.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show_confirm_box: false,
    fadeIn: '',
    show_tj: false,//是否显示推荐信息
    tj_info: { //推荐活动信息
      title: '户外拓展训练',//标题
      des: '户外拓展训练户外拓展训练户外拓展训练户外拓展训练',//描述
      date: '2018-06-25',//日期
      price: '0.00',//价格
    },//推荐信息
    info: { //保险信息    
    },
    price: 0.00,
    person_name: '',//姓名
    person_id: '',//身份证号
    beneficiary: '',//受益人姓名
    buy_time: '',//购买时长
    person_phone: '',//联系电话
    active_index: 0,//当前的购买市场的下标  默认0
  },
  industry: function (e) {
    this.setData({
      industry: e.detail.value
    })
  },
  change_person_name(e) { //更改姓名
    this.setData({
      person_name: e.detail.value
    })
  },
  change_person_id(e) { //更改身份证
    this.setData({
      person_id: e.detail.value
    })
  },
  // change_beneficiary(e) {  //更改受益人
  //   this.setData({
  //     beneficiary: e.detail.value
  //   })
  // },
  change_buy_time(e) {   //更改购买时长   
    var index = e.currentTarget.dataset.index;
    var price = this.data.info.price[index];
    this.setData({
      active_index: index,
      buy_time: this.data.info.time[index],
      price: price,
    })
  },
  change_person_phone(e) {  //更改联系电话
    this.setData({
      person_phone: e.detail.value
    })
  },
  close_mask() {  //关闭推荐信息遮罩层    
    var animation = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    this.animation = animation;
    animation.translate(0, 0).opacity(0).step()
    this.setData({
      animationData: animation.export()
    })
    setTimeout(() => {
      this.setData({
        show_tj: false
      })
    }, 300)
  },
  go_detail() {  //了解更多 跳转 活动详情
    app.navTo('../active_detail/active_detail?id=' + this.data.tj_info.id)
  },
  userinfos() {
    var that = this

  },
  submit_this() {  //提交信息
    if (!this.data.person_name || !this.data.person_id || !this.data.person_phone || !this.data.industry) {
      wx.showToast({
        title: '请输入完整信息',
        icon: 'none'
      });
      return;
    }
    if (!tools.test_phone(this.data.person_phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
    }
   
    
    let is_card = this.isCardNo(this.data.person_id)
    if (!is_card){
      wx.showToast({
        title: '请输入正确的身份证号码',
        icon: 'none'
      })
      return;
    }
    
    var that = this;
    //查看该用户是否是黄金会员
    app.request({
      url: router.user.info,
      method: "POST",
      data: {
        userId: wx.getStorageSync('userInfo')['id']
      },
      success: function (user_data) {
        console.log(user_data)
        if (user_data.user_list.grade == 3) {
          wx.showToast({
            title: '黄金VIP无需买',
          })
          return false
        } else {
          // 提交信息
          app.request({
            url: router.safe.add,
            method: "POST",
            data: {
              safe_id: that.data.safe_id,
              user_id: wx.getStorageSync("userInfo")['id'],
              name: that.data.person_name,
              id_card: that.data.person_id,
              industry: that.data.industry,
              phone: that.data.person_phone,
              time: that.data.buy_time,
              price: that.data.price,
            },
            success: function (data) {
              console.log(data);
              //return false;
              if (data.status == true) {
                wx.showToast({
                  title: '操作成功',
                })
                if (data.list) {
                  if (data.list.id) {
                    that.setData({
                      tj_info: { //推荐活动信息
                        id: data.list.id,//标题
                        title: data.list.title,//标题
                        des: data.list.content,
                        date: data.list.add_time,
                        price: data.list.price,//价格
                        img: data.list.banner
                      },
                    });
                  }
                }
                if (data.info) {
                  that.setData({
                    pay_info: data.info
                  });
                }
                that.setData({
                  show_confirm_box: true
                });
                let animation = wx.createAnimation({
                  duration: '300',
                  timingFunction: 'ease',
                })
                animation.translate(0, -50).opacity(1).step()
                that.setData({
                  animationData: animation.export()
                })
                let animation2 = wx.createAnimation({
                  duration: '300',
                  timingFunction: 'ease',
                })
                animation2.opacity(1).step()
                that.setData({
                  fadeIn: animation2.export()
                })
              } else {
                wx.showToast({
                  title: '操作失败',
                  icon: 'none'
                });
              }
            }
          })
        }
      }
    })
  },
  isCardNo: function (card) {

    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(card) === false) {
       
      return false;
    }else{
      return true
    }
  },
  pay: function (data) {
    var payInfo = this.data.pay_info;
    if (!payInfo) {
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      });
      return false;
    }
    var that = this;
    wx.requestPayment({
      'timeStamp': payInfo.timeStamp,
      'nonceStr': payInfo.nonceStr,
      'package': payInfo.package,
      'signType': payInfo.signType,
      'paySign': payInfo.paySign,
      'success': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:ok') {
          wx.showToast({
            title: '支付成功',
            icon: 'none'
          })
          that.setData({
            show_confirm_box: false
          });
          that.showInfo();
        }
      },
      'fail': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:fail cancel') {
          wx.showToast({
            title: '取消支付',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      },
      'complete': function (res) {
      }
    })
  },

  showInfo: function () {
    if (this.data.tj_info.id) {
      this.setData({
        show_tj: true,
      });
      var animation = wx.createAnimation({
        duration: '300',
        timingFunction: 'ease',
      })
      this.animation = animation;
      animation.translate(0, -50).opacity(1).step()
      this.setData({
        animationData: animation.export()
      })
    } else {
      app.navTo('../mysafe/mysafe');
    }
  },

  clsoe_confirm_box() {
    let animation = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation.translate(0, 0).opacity(0).step()
    this.setData({
      animationData: animation.export()
    })
    let animation2 = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation2.opacity(0).step()
    this.setData({
      fadeIn: animation2.export()
    })
    setTimeout(() => {
      this.setData({
        show_confirm_box: false
      });
    }, 300)
    this.showInfo();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      safe_id: id
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var id = this.data.safe_id;
    this.getInfo(id);
  },
  getInfo: function (id) {
    var that = this;
    app.request({
      url: router.safe.index,
      method: "POST",
      data: {
        id: id,
      },
      success: function (data) {
        if (data.status == true) {
          that.setData({
            info: data.info,
            price: data.info.price[0],
            buy_time: data.info.time[0],
          });
        } else {
          wx.showToast({
            title: '获取信息失败',
            icon: 'none'
          });
        }
      }
    })
  }
})